package com.settlement.system.service.impl;

import com.settlement.system.model.entity.OdsOmsRetailReturnOrderMain;
import com.settlement.system.mapper.OdsOmsRetailReturnOrderMainMapper;
import com.settlement.system.service.OdsOmsRetailReturnOrderMainService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.settlement.system.model.query.OdsOmsRetailReturnOrderMainQuery;
import com.settlement.system.model.form.OdsOmsRetailReturnOrderMainForm;
import com.settlement.system.model.vo.OdsOmsRetailReturnOrderMainImportVo;
import org.springframework.beans.BeanUtils;
import org.springframework.util.Assert;
import com.settlement.system.common.util.StringUtils;

/**
 * <p>
 * 退单表 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2024-08-02
 */
@Service
public class OdsOmsRetailReturnOrderMainServiceImpl extends ServiceImpl<OdsOmsRetailReturnOrderMainMapper, OdsOmsRetailReturnOrderMain> implements OdsOmsRetailReturnOrderMainService {

    @Override
    public Page<OdsOmsRetailReturnOrderMain> getPage(OdsOmsRetailReturnOrderMainQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        // 查询数据
        Page<OdsOmsRetailReturnOrderMain> page = new Page<>(pageNum, pageSize);
        return this.baseMapper.getPage(page, queryParams);
    }

     @Override
     public boolean saveOdsOmsRetailReturnOrderMain(OdsOmsRetailReturnOrderMainForm odsOmsRetailReturnOrderMainForm) {
         OdsOmsRetailReturnOrderMain odsOmsRetailReturnOrderMain = new OdsOmsRetailReturnOrderMain();
         BeanUtils.copyProperties(odsOmsRetailReturnOrderMainForm, odsOmsRetailReturnOrderMain);
         return this.saveOrUpdate(odsOmsRetailReturnOrderMain);
     }

     @Override
     public OdsOmsRetailReturnOrderMainForm getOdsOmsRetailReturnOrderMainFormData(Long id) {
         OdsOmsRetailReturnOrderMain odsOmsRetailReturnOrderMain = this.getById(id);
         OdsOmsRetailReturnOrderMainForm odsOmsRetailReturnOrderMainForm = new OdsOmsRetailReturnOrderMainForm();
         BeanUtils.copyProperties(odsOmsRetailReturnOrderMain, odsOmsRetailReturnOrderMainForm);
         return odsOmsRetailReturnOrderMainForm;
     }

     @Override
     public boolean deleteOdsOmsRetailReturnOrderMain(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "删除的数据为空");
        List<Long> idArray = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(idArray);
     }

    @Override
    public void importData(List<OdsOmsRetailReturnOrderMainImportVo> voList)  {
        List<OdsOmsRetailReturnOrderMain> odsOmsRetailReturnOrderMainList = new ArrayList<>();
        for (OdsOmsRetailReturnOrderMainImportVo importVo : voList) {
            OdsOmsRetailReturnOrderMain odsOmsRetailReturnOrderMain = new OdsOmsRetailReturnOrderMain();
            BeanUtils.copyProperties(importVo, odsOmsRetailReturnOrderMain);
            odsOmsRetailReturnOrderMainList.add(odsOmsRetailReturnOrderMain);
        }
        this.saveOrUpdateBatch(odsOmsRetailReturnOrderMainList);
    }

    @Override
    public List<OdsOmsRetailReturnOrderMainImportVo> findByIds(String idsStr) {
        if (!StringUtils.hasText(idsStr)) {
            return new ArrayList<>();
        }
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        List<OdsOmsRetailReturnOrderMain> odsOmsRetailReturnOrderMainList = this.getBaseMapper().selectBatchIds(ids);
        List<OdsOmsRetailReturnOrderMainImportVo> voList = new ArrayList<>();
        for (OdsOmsRetailReturnOrderMain odsOmsRetailReturnOrderMain : odsOmsRetailReturnOrderMainList) {
            OdsOmsRetailReturnOrderMainImportVo vo = new OdsOmsRetailReturnOrderMainImportVo();
            BeanUtils.copyProperties(odsOmsRetailReturnOrderMain, vo);
            voList.add(vo);
        }
        return voList;
    }
}
